<?php
require_once("../includes/func.php");
require_once("../includes/themes/" . $myTheme . "/head.php");


echo '<div class="homebody"><div class="center">';
echo '<h2>Новости Проекта</h2>';

$act = intval($_GET['act']);
switch ($act) {
default:
	$myView = new Pager; //pagination
	$myView -> page_calculate("blog", " ORDER BY `id` DESC ");
	
	$query1 = $myView -> page_list();
	while ($data1 = mysql_fetch_array($query1)) {
		echo '<div class="newsblog"><a href="'. HOME . MYDIR .'apanel/news.php?act=3&amp;b='. $data1['id'] .'">';
		echo '<h3>'. $data1['title'] .'</h3></a>';
		echo '<h4>Опубликовано: '. $data1['date'] .'</h4>';
		echo $data1['message'] .'<br />';
		// Output Tags begin
		echo '<h4>Метки: ';
		$getNews = new GetNews;
		$getNews -> printTags($data1['tags']);
		// Output tags end
		echo '</h4><h4 style="text-align: right;"><a href="news.php?act=3&amp;b='. $data1['id'] .'">Комментариев: ' .$data1['comments'] .'</a> ';
		echo 'Просмотров: ' .$data1['see'] .'';
		echo '<h3><a href="'. HOME . MYDIR .'apanel/news.php?act=5&amp;b='. $data1['id'] .'">Редактировать</a> | ';
		echo '<a href="'. HOME . MYDIR .'apanel/news.php?act=6&amp;b='. $data1['id'] .'" class="delete">Удалить</a></h3>';
		echo '</h4></div>';
	}
	$myView -> page_footer();
	?>
	<script type="text/javascript">
		
		$(".delete").click( function() {
			$(this).load( $(this).attr('href') + " .message");
			return false;
		});
		
	</script>
	<?php
	break;
	
case 3: //topic body
	$b = intval($_GET['b']);
	$myView = new Pager;
	
	$myCount = $myView -> get_count("blog", "WHERE `id` = '". $b ."'");
	if ( $myCount == 0 ) //if no news
	{
		echo '<div id="login"><td class="message">';
		echo 'Запись не существует.';
		echo '</td></div>';
		break;
	}
	
	
	//increase views
	mysql_query("UPDATE `blog` SET `see` = `see`+ 1 WHERE `id` = '". $b ."'");
	
	$query1 = $myView -> sql("SELECT `title`, `message`, `comments`, `see`, `date`, `tags` FROM `blog` WHERE `id` = '" .$b. "' LIMIT 1");
	$data1 = mysql_fetch_array($query1);
	
	echo '<h3>'. $data1['title'] .'</h3>';
	echo '<h4>Опубликовал: Администратор</h4>';
	echo '<h3>';
	echo $data1['message'] .'<br/>';
	echo '</h3><h4>Просмотров: '. $data1['see'];
	echo '<br/>Дата: '. $data1['date'];
		
		// Output Tags begin
		echo '<br />Метки: ';
		$tags = explode(', ', $data1['tags']);
		foreach($tags as $tag) 
		{
			echo '<a href="'. $tag .'">'. $tag .'</a> ';
		}
		// Output Tags end
		
	echo '</h4>';
	?>

	<noindex>
		<script src="http://s7.addthis.com/js/250/addthis_widget.js"></script>
		<p>Опубликовать:</p>
		<div class="addthis_toolbox addthis_default_style">
			<a class="addthis_button_vk"></a>    
			<a class="addthis_button_facebook"></a>    
			<a class="addthis_button_twitter"></a>
			<a class="addthis_button_googlebuzz"></a>
			<a class="addthis_button_email"></a>
			<a class="addthis_button_google"></a>
			<a class="addthis_button_compact"></a>
		</div>
	</noindex>

	<?php
	echo '<h3>Комментарии:</h3>';
	//load comments
	$myView -> page_calculate("comments", "WHERE `post_id` = '". $b ."' AND `stat` = 1 ORDER BY `id` DESC");
	$query2 = $myView -> page_list();
	
	while ($data2 = mysql_fetch_array($query2)) {
		++$i;
		if ($i % 2) 
			echo '<div class="binar1"><b>';
		else
			echo '<div class="binar2"><b>';
		echo $data2['comment'] .'</b><br />';
		echo '<h4>Автор: '. $data2['author'] ;
		echo ', Из: '. $data2['local'] ;
		echo ' ('. $data2['date'] .')';
		echo '</h4>';
		echo '</div>';
	}
	
	//write comment
	echo '<form method="post" action="'. HOME . MYDIR .'apanel/news.php?act=4&amp;blogger='. $data1['author'] .'&amp;post_id='. $b .'" ><h3>';
		echo 'Введите текст комментария ниже:';
		echo '</h3><input type="text" name="username" size="20" maxlength="140" value="" /> Ваше имя<br />';
		echo '<input type="text" name="mail" size="20" maxlength="140" value="" /> Почтовый ящик<br />';
		echo '<textarea rows="10" cols="45" name="comment"></textarea>';
		echo '<br/><input type="submit" name="submit" class="submit-botton" value="Отправить" />';
	echo '</form>';
	
	$myView -> page_footer();
	
	break;
case 4:
	$pager = new Posting;
	
	echo '<div class="message">'. $pager -> commentAdd($_POST['comment'], $_GET['post_id'], $_POST['mail'], $_POST['username']) .'</div>';
	
	break;
case 5:
	//edit
	$make = $_GET['make'];
	$postId = intval($_GET['b']);
	
	switch ($make)
	{
	default:
		$getNews = new GetNews;
		$getNews -> getPostData($postId);
		?>
		<script type="text/javascript" src="../includes/modules/wysiwyg/tiny_mce.js"></script>
		<script type="text/javascript">
		tinyMCE.init({
			// General options
			mode : "textareas",
			theme : "advanced",
			plugins : "pagebreak,style,layer,table,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

			// Theme options
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Skin options
			skin : "o2k7",
			skin_variant : "silver",

			// Example content CSS (should be your site CSS)
			content_css : "/includes/themes/minimum/meta.css",

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "js/template_list.js",
			external_link_list_url : "js/link_list.js",
			external_image_list_url : "js/image_list.js",
			media_external_list_url : "js/media_list.js",

			// Replace values for the template plugin
			template_replace_values : {
					username : "Some User",
					staffid : "991234"
			}
		});
		</script>
		<script type="text/javascript">
		var rusChars = new Array('а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ч','ц','ш','щ','э','ю','\я','ы','ъ','ь', ' ', '\'', '\"', '\#', '\$', '\%', '\&', '\*', '\,', '\:', '\;', '\<', '\>', '\?', '\[', '\]', '\^', '\{', '\}', '\|', '\!', '\@', '\(', '\)', '\-', '\=', '\+', '\/', '\\', '/ +/g');
		var transChars = new Array('a','b','v','g','d','e','jo','zh','z','i','j','k','l','m','n','o','p','r','s','t','u','f','h','ch','c','sh','csh','e','ju','ja','y','', '', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '+');
		var from = "";
		function convert2EN()
		{
			from = document.getElementById("postTitle").value;
			from = from.toLowerCase();
			var to = "";
			var len = from.length;
			var character, isRus;
			for(var i=0; i < len; i++)
			{
				character = from.charAt(i,1);
				isRus = false;
				for(var j=0; j < rusChars.length; j++)
				{
					if(character == rusChars[j])
					{
						isRus = true;
						break;
					}
				}
				to += (isRus) ? transChars[j] : character;
			}
			document.getElementById("url").value = to;
		}
		</script>

		
		<form method="post" action="<?php echo HOME . MYDIR; ?>apanel/news.php?act=5&amp;b=<?php echo $postId; ?>&amp;make=save" >
			<b>Тема</b><br />
			<input type="text" onKeyUp="convert2EN();" onChange="convert2EN();" onBlur="convert2EN();" id="postTitle" name="title" size="76" value="<?php echo $getNews -> postTitle; ?>" /><br />
			<b>Чпу</b><br />
			<input type="text" id="url" name="url" size="76" value="<?php echo $getNews -> postUrl; ?>" /><br />
			<b>Метки</b> (через запятую)<br />
			<input type="text" name="tags" size="76" value="<?php echo $getNews -> postTags; ?>" /><br />
			<b>Полный текст</b><br />
			<textarea rows="20" cols="100" name="message"><?php echo $getNews -> postMessage; ?></textarea><br/>
			<input type="submit" name="submit" class="submit-botton" value="Отправить" />
		</form>
		<?php
		break;
	case 'save':
		$message = trim($_POST['message']);
		$title = trim($_POST['title']);
		$tags = trim($_POST['tags']);
		$url = clear_url_tab($_POST['url']);
		
		
		$poster = new Posting;
		echo '<div class="message">'. $poster -> updatePost($postId, $message, $title, $tags, $url) .'</div>';
		break;
	}
	break;
case 6:
	//delete
	$postId = $_GET['b'];
	
	$posting = new Posting;
	echo '<div class="message">'. $posting -> deletePost($postId) .'</div>';
	
	break;
}


echo '</div>';
require_once ('../includes/themes/' . $myTheme . '/end.php');
?>